package com.nuonuo.nuo_xp;

/**
 * @author glsite.com
 * @version $Rev$
 * @des ${TODO}
 * @updateAuthor $Author$
 * @updateDes ${TODO}
 */


import android.util.Base64;
import android.util.Log;
import java.math.BigInteger;
import de.robv.android.xposed.IXposedHookLoadPackage;
import de.robv.android.xposed.XC_MethodHook;
import de.robv.android.xposed.XposedBridge;
import de.robv.android.xposed.XposedHelpers;
import de.robv.android.xposed.callbacks.XC_LoadPackage;

/**
 * Hook通杀RSA
 *java.security.spec.X509EncodedKeySpec
 */
public class HOOK_RSA implements IXposedHookLoadPackage {
    private String log_d_tag="XP";
    @Override
    public void handleLoadPackage(XC_LoadPackage.LoadPackageParam loadPackageParam) throws Throwable {
        Log.d(log_d_tag,"注入成功...");
        Log.d(log_d_tag,"***rsa算法***");
        Log.d(log_d_tag,"HOOK_class:java.security.spec.X509EncodedKeySpec");
        Log.d(log_d_tag,"HOOK_class:java.security.spec.PKCS8EncodedKeySpec");
        Log.d(log_d_tag,"HOOK_class:java.security.spec.RSAPublicKeySpec");
        Log.d(log_d_tag,"HOOK_class:java.security.spec.RSAPublicKeySpec");
        Log.d(log_d_tag,"活动APP包名:"+loadPackageParam.packageName);

        //Hook 密钥 RSA公钥 X509EncodedKeySpec(byte[] encodedKey)
        XposedBridge.hookAllConstructors(XposedHelpers.findClass("java.security.spec.X509EncodedKeySpec",
                loadPackageParam.classLoader),  new XC_MethodHook() {
            @Override
            protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
                byte[] X509EncodedKeySpec = (byte[]) param.args[0];//第一个必为密钥
                String algorithm = "RSA公钥";
                //String data = new String(X509EncodedKeySpec);
                //String dataHex = Utils.byteToHexString(X509EncodedKeySpec);
                String dataBase64 = Base64.encodeToString(X509EncodedKeySpec,0);
                Log.d(log_d_tag,"-");
                Log.d(log_d_tag,"-");
                Log.d(log_d_tag,"=====================================HOOK开始==========================================");
                Log.d(log_d_tag,"=====================================Class_RSA==========================================");
                Log.d(log_d_tag,"======================================X509EncodedKeySpec==========================================");

                Log.d(log_d_tag,algorithm+"_rasKey_Data_Base64:"+dataBase64);
                Log.d(log_d_tag,"=======================================RSA公钥=========================================");
                Log.d(log_d_tag,"=====================================调用堆栈~开始==============================================");
                Log.d(log_d_tag,Log.getStackTraceString(new Throwable()));
                Log.d(log_d_tag,"=====================================调用堆栈~结束==============================================");
            }
        });

        //Hook 密钥 RSA私钥 PKCS8EncodedKeySpec(byte[] encodedKey)
        XposedBridge.hookAllConstructors(XposedHelpers.findClass("java.security.spec.PKCS8EncodedKeySpec",
                loadPackageParam.classLoader),  new XC_MethodHook() {
            @Override
            protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
                byte[] PKCS8EncodedKeySpec = (byte[]) param.args[0];//第一个必为密钥
                String algorithm = "RSA私钥";
                String data = new String(PKCS8EncodedKeySpec);
                String dataHex = Utils.byteToHexString(PKCS8EncodedKeySpec);
                String dataBase64 = Base64.encodeToString(PKCS8EncodedKeySpec,0);
                Log.d(log_d_tag,"-");
                Log.d(log_d_tag,"-");
                Log.d(log_d_tag,"====================================HOOK开始==========================================");
                Log.d(log_d_tag,"=====================================Class_RSA==========================================");
                Log.d(log_d_tag,"=====================================PKCS8EncodedKeySpec==========================================");
                Log.d(log_d_tag,algorithm+"_rasKeyDataBase64:"+dataBase64);
                Log.d(log_d_tag,"======================================RSA私钥=========================================");
                Log.d(log_d_tag,"=====================================调用堆栈~开始==============================================");
                Log.d(log_d_tag,Log.getStackTraceString(new Throwable()));
                Log.d(log_d_tag,"=====================================调用堆栈~结束==============================================");
            }
        });
        //Hook RAS 16进制密钥 RSAPublicKeySpec(BigInteger modulus, BigInteger publicExponent)
        XposedBridge.hookAllConstructors(XposedHelpers.findClass("java.security.spec.RSAPublicKeySpec",
                loadPackageParam.classLoader),  new XC_MethodHook() {
            @Override
            protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
                String modulus = ((BigInteger) param.args[0]).toString(16);//加密模式N
                String publicExponent = ((BigInteger) param.args[1]).toString(16);//密钥E
                String algorithm = "RSA 16进制密钥";
                Log.d(log_d_tag,"-");
                Log.d(log_d_tag,"-");
                Log.d(log_d_tag,"========================================HOOK开始==========================================");
                Log.d(log_d_tag,"========================================Class_RSA==========================================");
                Log.d(log_d_tag,"========================================RSAPublicKeySpec==========================================");
                Log.d(log_d_tag,algorithm+"_rasKeyDataModulus:"+modulus);
                Log.d(log_d_tag,algorithm+"_rasKeyDataPublicExponent:"+publicExponent);
                Log.d(log_d_tag,"====================================RSA16进制密钥=========================================");
                Log.d(log_d_tag,"=====================================调用堆栈~开始==============================================");
                Log.d(log_d_tag,Log.getStackTraceString(new Throwable()));
                Log.d(log_d_tag,"=====================================调用堆栈~结束==============================================");
            }
        });
    }
}
